package com.detroitchowfree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.lang3.StringUtils;

import com.detroitchowfree.StoreVO;
import com.detroitchowfree.TagCollection;

public class DetroitChowUtils {
	
	public static ArrayList<TagCollection> buildMasterList(ArrayList<StoreVO> ar) {
		if(ar == null)
			return null;
		
		ArrayList<TagCollection> result = new ArrayList<TagCollection>();
		ArrayList<TagCollection> tagCollections = new ArrayList<TagCollection>();
		TagCollection allCollection = new TagCollection();
		allCollection.tag=StoreService.ALL_TAG;
		allCollection.stores = new ArrayList<StoreVO>();
		boolean isTagCreated = false;
		
		for(StoreVO svo : ar) {
			allCollection.stores.add(svo);
			if(svo.tags != null){
				
				for(String _tag: svo.tags) {
					if(_tag == null )
						isTagCreated=true;
					else{
						
						isTagCreated=false;
						for(TagCollection tc : tagCollections) {
							//LogUtil.log("buildMasterList new tag", _tag + " tagCollection Tag: " + tc.tag);
							if(tc.tag.equals(_tag)) {
								isTagCreated = true;
								tc.stores.add(svo);
							}
						}
						
					}
					
					
					
					if(!isTagCreated) {
						
						TagCollection tmpTc = new TagCollection();
						tmpTc.tag = _tag;
						tmpTc.stores = new ArrayList<StoreVO>();
						tmpTc.stores.add(svo);
						tagCollections.add(tmpTc);
					}
					
					
				}
			}
		}
		
		result.add(allCollection);
		result.addAll(tagCollections);
		return result;
		
	}

	public static ArrayList<String> getAllStoreNames(ArrayList<StoreVO> ar) {
	  // TODO Auto-generated method stub
		if(ar == null){
			return null;
		}
		
		ArrayList<String> result = new ArrayList<String>();
		for(StoreVO svo : ar){
			result.add(svo.name);
		}
	  return result;
  }

	public static  ArrayList<String> getAllCities(ArrayList<StoreVO> ar) {
		if(ar == null){
			return null;
		}
		
		//ArrayList<String> result = new ArrayList<String>();
		HashSet result = new HashSet();
		for(StoreVO svo : ar){
			result.add(svo.city);
		}
	  return new ArrayList(Arrays.asList(result.toArray()));
  }

	public static ArrayList<StoreVO> getSearchStoreNames(ArrayList<StoreVO> ar, String s) {
		if(ar == null || s == null)
			return null;
		ArrayList<StoreVO> result = null;
		for(StoreVO svo : ar){
			if(StringUtils.contains(svo.name.toUpperCase(), s.toUpperCase())) {
				if(result == null)
					result = new ArrayList<StoreVO>();
				result.add(svo);
			}
		}
	  return result;
  }

	public static ArrayList<StoreVO> getSearchCities(ArrayList<StoreVO> currentResult, ArrayList<StoreVO> ar,String s) {
		if(ar == null || s == null)
			return null;
		ArrayList<StoreVO> result = null;
		for(StoreVO svo : ar){
			if(StringUtils.contains(svo.city.toUpperCase(), s.toUpperCase())) {
				
				if(result == null)
					result = new ArrayList<StoreVO>();
				
				result.add(svo);
			}
		}
	  return result;
  }

	public static ArrayList<StoreVO> getSearchTags(ArrayList<StoreVO> currentResult, ArrayList<StoreVO> ar,String s) {
		ArrayList<StoreVO> result = null;
		for(StoreVO svo : ar){
			for(String tag : svo.tags){
				if(StringUtils.contains(tag.toUpperCase(), s.toUpperCase())) {
					
					if(result == null)
						result = new ArrayList<StoreVO>();
					
					result.add(svo);
					break;
				}
			}
		}
	  return result;
  }
	
	public static void main(String [] args) {
		ArrayList<StoreVO> ar = new ArrayList<StoreVO>();
		StoreVO svo = new StoreVO();
		svo.name = "Test 1";
		svo.tags = new ArrayList<String>();
		svo.tags.add("Seafood");
		ar.add(svo);
		
		svo = new StoreVO();
		svo.name = "Test 2";
		svo.tags = new ArrayList<String>();
		svo.tags.add("Pizza");
		ar.add(svo);
		
		svo = new StoreVO();
		svo.name = "Test 3";
		svo.tags = new ArrayList<String>();
		svo.tags.add("Seafood");
		ar.add(svo);
		
		ArrayList<TagCollection> thing =  buildMasterList(ar);
		for(TagCollection tc : thing) {
			System.out.println("tc tag: " + tc.tag);
			System.out.println("tc size: " + tc.stores.size());
		}
		
	}
	
	public static String formattedPhoneNumber(String s) {
		
		if(s == null)
			return s;
		String result = null;
		if(s.length() ==10) {
			
			result ="(" + s.substring(0, 3) + ") ";
			result += s.substring(3,6) + "-";
			result += s.substring(6,10);
			
		}else if(s.length() ==7) {
			
			result = s.substring(0,3) + "-";
			result += s.substring(3,7);
			
		}else
			result = s;
		return result;
	}


}
